<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of XinqingprobabilityLogic
 *
 * @author Administrator
 */
class XinqingprobabilityLogic extends BaseLogic{
    //put your code here
    private $xinqingprobabilityTable;
    public function __construct() {
        parent::__construct();
        $this->xinqingprobabilityTable=  $this->load("xinqingprobability");
        $this->table=  $this->xinqingprobabilityTable;
    }
    /**
     * 判断用户心情概率是否存在
     * @param type $user_id   用户id
     * @param type $xinqing_id  心情id
     * @param type $month  月份
     * @param type $day  日期
     * @return boolean  存在返回true 否则返回false
     */
    public function exist_probability($user_id,$xinqing_id,$month,$day){
        $count=$this->xinqingprobabilityTable->get_count("user_id={$user_id} and xinqing_id={$xinqing_id} and `month`={$month} and `day`={$day}");
        if($count>0){
            return TRUE;
        }
        return FALSE;
    }
    /**
     * 获得用户心情概率
     * @param type $user_id  用户id
     * @param type $xinqing_id  心情id
     * @param type $month  月份
     * @param type $day  日期
     * @return type  返回用户心情概率
     */
    public function getProbability($user_id,$xinqing_id,$month,$day){
        return $this->getOne("user_id={$user_id} and xinqing_id={$xinqing_id} and `month`={$month} and `day`={$day}");
    }
    /**
     * 平均温度当前设置的日期下是否更新过，是返回true 否返回false
     * @param type $v  其中必须包含user_id，xinqing_id，month，day,temp
     * @return boolean 更新过了返回true,否则返回false
     */
    public function isUpdate($v){
        $edit_time=date("Y-m-d");
        $c=  $this->xinqingprobabilityTable->get_count("user_id={$v["user_id"]} and xinqing_id={$v["xinqing_id"]} and month={$v["month"]} and day={$v["day"]} and avg_temp_edit_time='{$edit_time}'");
        if($c>0){
            return TRUE;
        }
        return FALSE;
    }
    /**
     * 概率计算当前设置的日期下是否更新过，是返回true 否返回false
     * @param type $v  其中必须包含user_id，xinqing_id，month，day,temp
     * @return boolean 更新过了返回true,否则返回false
     */
    public function probIsUpdate($v){
        $edit_time=date("Y-m-d");
        $c=  $this->xinqingprobabilityTable->get_count("user_id={$v["user_id"]} and xinqing_id={$v["xinqing_id"]} and month={$v["month"]} and day={$v["day"]} and prob_edit_time='{$edit_time}'");
        if($c>0){
            return TRUE;
        }
        return FALSE;
    }
     /**
     * 概率计算当前设置的日期(当天实时)下是否更新过，是返回true 否返回false
     * @param type $v  其中必须包含user_id，xinqing_id，month，day,temp
     * @return boolean 更新过了返回true,否则返回false
     */
    public function probDayIsUpdate($v){
        $edit_time=date("Y-m-d");
        $c=  $this->xinqingprobabilityTable->get_count("user_id={$v["user_id"]} and xinqing_id={$v["xinqing_id"]} and month={$v["month"]} and day={$v["day"]} and day_prob_edit_time='{$edit_time}'");
        if($c>0){
            return TRUE;
        }
        return FALSE;
    }
    /**
     * 计算用户的平均温度
     * @param type $user_id 用户id
     * @param type $month  月份
     * @param type $day    日期
     */
    public function avgTemp($user_id,$month,$day){
        if($user_id>0){
            $sql="select a.*,l.base_name as xinqing_name,u.nickname from (SELECT avg(temp) as avg_temp,user_id,xinqing_id,`month`,`day` from jp_xinqing_temp WHERE user_id={$user_id} and `month`={$month} and `day`={$day} GROUP BY xinqing_id) as a LEFT JOIN jp_xinqinglist as l on a.xinqing_id=l.id LEFT JOIN jp_user as u on a.user_id=u.id";
             $row=$this->xinqingprobabilityTable->execute($sql);
             foreach ($row as $v){
                    $v["avg_temp_edit_time"]=  date("Y-m-d");
                    if($this->isUpdate($v)){
                        continue;
                    }
                    if($this->exist_probability($v["user_id"],$v["xinqing_id"],$v["month"],$v["day"])){
                        $p=  $this->getProbability($v["user_id"],$v["xinqing_id"],$v["month"],$v["day"]);
                        $this->save($v,$p["id"]);
                    }else{
                        $this->save($v);
                    }
             }
        }
    }
    /**
     * 计算心情概率（所设置的日期下 历年来）
     * @param type $user_id  用户id
     * @param type $month   月份
     * @param type $day     日期
     */
    public function probXinqing($user_id,$month,$day){
        if($user_id>0){
            $sql="select a.*,a.prob/b.allcount as p,b.allcount as allcount from(select count(xinqing_id) as prob,user_id,xinqing_id,`month`,`day`,xinqing_name,nickname from jp_user_xinqing where user_id={$user_id} and `month`={$month} and `day`={$day} and status=1 GROUP BY xinqing_id)as a LEFT JOIN (SELECT COUNT(xinqing_id) as allcount,user_id,`month`,`day` from jp_user_xinqing where user_id={$user_id} and `month`={$month} and `day`={$day} and status=1) as b on a.user_id=b.user_id and a.`month`=b.`month` and a.`day`=b.`day`";
            $row=$this->xinqingprobabilityTable->execute($sql);
            $dateinfo=  getdate();
            foreach ($row as $v){
                $data=array(
                    "user_id"=>$v["user_id"],
                    "xinqing_name"=>$v["xinqing_name"],
                    "nickname"=>$v["nickname"],
                    "xinqing_id"=>$v["xinqing_id"],
                    "prob"=>$v["p"],
                    "prob_num"=>$v["prob"],
                    "prob_all"=>$v["allcount"],
                    "month"=>$v["month"],
                    "day"=>$v["day"],
                    "prob_edit_time"=>date("Y-m-d"),
                );
                if(($dateinfo["mon"]+$dateinfo["mday"])!=($month+$day)&&$this->probIsUpdate($data)){
                   continue;
                }
                if($this->exist_probability($v["user_id"],$v["xinqing_id"],$v["month"],$v["day"])){
                        $prob=$this->getProbability($v["user_id"],$v["xinqing_id"],$v["month"],$v["day"]);
                        if($prob&&isset($prob["id"])){
                           $this->save($data,$prob["id"]);
                        }
                   
                }else{
                    $this->save($data);
                }
               
            
        }
      }
    }
    
    
    /**
     * 计算心情概率（所设置的日期下,实时，当天时间下的）
     * @param type $user_id  用户id
     * @param type $year    年份
     * @param type $month   月份
     * @param type $day     日期
     */
    public function probDayXinqing($user_id,$year,$month,$day){
        if($user_id>0){
            $clearsql="update jp_xinqingprobability set day_prob_num=0,day_prob_all=0,day_prob=0 where user_id={$user_id} and `month`={$month} and `day`={$day} and day_prob_edit_time!='".  date("Y-m-d")."'";
            $this->xinqingprobabilityTable->query($clearsql);
            $sql="select a.*,a.prob/b.allcount as p,b.allcount as allcount from(select count(xinqing_id) as prob,user_id,xinqing_id,`month`,`day`,xinqing_name,nickname from jp_user_xinqing where user_id={$user_id} and `month`={$month} and `day`={$day} and `year`={$year} and status=1 GROUP BY xinqing_id)as a LEFT JOIN (SELECT COUNT(xinqing_id) as allcount,user_id,`month`,`day` from jp_user_xinqing where user_id={$user_id} and `month`={$month} and `day`={$day} and `year`={$year} and status=1) as b on a.user_id=b.user_id and a.`month`=b.`month` and a.`day`=b.`day`";
            $row=$this->xinqingprobabilityTable->execute($sql);
            $dateinfo=  getdate();
            foreach ($row as $v){
                $data=array(
                    "user_id"=>$v["user_id"],
                    "xinqing_name"=>$v["xinqing_name"],
                    "nickname"=>$v["nickname"],
                    "xinqing_id"=>$v["xinqing_id"],
                    "day_prob"=>$v["p"],
                    "day_prob_num"=>$v["prob"],
                    "day_prob_all"=>$v["allcount"],
                    "month"=>$v["month"],
                    "day"=>$v["day"],
                    "day_prob_edit_time"=>date("Y-m-d"),
                );
                
                if(($dateinfo["mon"]+$dateinfo["mday"])!=($month+$day)&&$this->probDayIsUpdate($data)){
                   continue;
                }
               
                if($this->exist_probability($v["user_id"],$v["xinqing_id"],$v["month"],$v["day"])){
                        $prob=$this->getProbability($v["user_id"],$v["xinqing_id"],$v["month"],$v["day"]);
                        if($prob&&isset($prob["id"])){
                           $this->save($data,$prob["id"]);
                        }
                   
                }else{
                    $this->save($data);
                }
               
            
        }
      }
    }
    /**
     * 计算总概率值
     * @param type $user_id 用户id
     * @param type $month   月份
     * @param type $day     日期
     */
    public function jiSuanTotal($user_id,$month,$day){
        if($user_id>0){
            $sql="update jp_xinqingprobability set total=prob+day_prob*10+hum_prob+temp_weight where user_id={$user_id} and month={$month} and day={$day}";
            $this->xinqingprobabilityTable->query($sql);
        }
    }
    /**
     * 计算用户心情温度权重值
     * @param type $user_id 用户id
     */
    public function jiSuanTempWeight($user_id){
        if($user_id>0){
            $todaydateinfo=  getdate();
            $userLogic=  $this->uselogic("User");
            $xinqingtempLogic=  $this->uselogic("Xinqingtemp");
            $todaydateinfo=  getdate();
            //获得当天天气
            $weather=$userLogic->getUserWeather($user_id,$todaydateinfo["year"],$todaydateinfo["mon"],$todaydateinfo["mday"]);
            if($weather){
                $temp=$weather["temp"];
                //获得该温度下的温度权重值和心情id

                $weight=$xinqingtempLogic->getTempWeight($user_id,$temp,$todaydateinfo["mon"],$todaydateinfo["mday"]);
                $log=Logclass::getInstance();
                $log->writeline("自定义记录日志:weight:".$weight["weight"].",xinqing_id=".$weight["xinqing_id"]);
                if($weight){
                    $this->xinqingprobabilityTable->update("temp_weight=0.0", "user_id={$user_id} and month={$todaydateinfo["mon"]} and day={$todaydateinfo["mday"]}");
                    $pro=$this->xinqingprobabilityTable->get_one("*", "user_id={$user_id} and xinqing_id={$weight["xinqing_id"]} and month={$todaydateinfo["mon"]} and day={$todaydateinfo["mday"]}");
                    if($pro){
                        $updatedata=array(
                            "temp_weight"=>$weight["weight"]
                        );
                        
                        $this->save($updatedata,$pro["id"]);
                    }
                }
                
            }
        }
    }
    /**
     * 获得用户的心情信息 所设置日期下的
     * @param type $user_id   用户id
     * @param type $month     月份
     * @param type $day       日期
     * @return boolean        返回 用户心情信息，否则返回false
     */
    public function getUserXinQing($user_id,$month,$day){
        if($user_id>0){
            $max=$this->xinqingprobabilityTable->get_one("*", "user_id={$user_id} and month={$month} and day={$day} order by total desc");
            if($max){
                $list=  $this->xinqingprobabilityTable->select("*","user_id={$user_id} and month={$month} and day={$day} and total={$max["total"]} order by total desc");
                if($list){
                    $key=  array_rand($list,1);
                    return $list[$key];
                }
                
                
            }
        }
        return FALSE;
    }
    
}
